<?php

namespace app\index\controller;

use app\common\CommonResponse;
use app\index\validate\NoticeValidate;
use think\App;
use think\Controller;
use think\Request;

class Notice extends Base
{

    protected $noticeModel;
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->noticeModel=new \app\common\model\Notice();
    }

    public function list(){
        $notices=$this->noticeModel->order("create_time")->paginate(10);
        $this->assign("notices",$notices);
        return $this->fetch();
    }

    public function addNotice(Request $request){
        if($request->isPost()){
            $notice=$request->post();
            $noticeValidate=new NoticeValidate();
            $check = $noticeValidate->check($notice);
            if(!$check){
                return $this->error($noticeValidate->getError());
            }
            $notice['create_time']=time();
            $save = $this->noticeModel->save($notice);
            if(!$save){
                return $this->error("新增通知失败");
            }
            return $this->success("新增通知成功","list");
        }
        return $this->fetch();

    }

    public function delNotice(Request $request){
        if(!$request->post("id")){
            return json(CommonResponse::error("通知id不能为空"));
        }
        $id=$request->post("id");
        $res = $this->noticeModel::get($id)->delete();
        if(!$res){
            return json(CommonResponse::error("通知删除失败"));
        }
        return json(CommonResponse::success("通知删除成功"));

    }

}
